package cn.javaeye.lonlysky.lforum.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.ParentPackage;
import org.xidea.lite.servlet.DateUtils;
import org.xidea.lite.servlet.FCKeditorWrapper;

import cn.javaeye.lonlysky.lforum.ForumBaseAction;
import cn.javaeye.lonlysky.lforum.comm.LForumRequest;
import cn.javaeye.lonlysky.lforum.entity.forum.Users;
import cn.javaeye.lonlysky.lforum.entity.web.FartherType;
import cn.javaeye.lonlysky.lforum.entity.web.Info;
import cn.javaeye.lonlysky.lforum.entity.web.ParentType;
import cn.javaeye.lonlysky.lforum.service.FartherTypeManager;
import cn.javaeye.lonlysky.lforum.service.InfoManager;
import cn.javaeye.lonlysky.lforum.service.ParentTypeManager;
import cn.javaeye.lonlysky.lforum.service.UserManager;

/**
 * 分栏模式首页
 * 
 * @author fishkang
 * 
 */
@SuppressWarnings("unchecked")
@ParentPackage("default")
public class addInfoAction extends ForumBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6321220918122218211L;
	/**
	 * 
	 */
	private ParentType parentType;
	private FartherType fartherType;
	private Info info;
	private List<Info> infoList = new ArrayList();
	private Users user;
	/*
	 * private String editor;
	 */
	private Integer pid;
	private InfoManager infoManager;

	private UserManager userManager;
	private FartherTypeManager fartherTypeManager;
	private ParentTypeManager parentTypeManager;
	private List<FartherType> fartherlist;
	private List<ParentType> parentTypelist;
	private File fileUpload;// 获取上传文件
	private String fileUploadContentType;// 获取上传文件的类型，注意上传类型变量命名方式
	private String fileUploadFileName;// 获取上传文件的名称

	@Override
	public String execute() throws Exception {
		if (LForumRequest.getParamValue("list").equals("true")) {
			infoList = infoManager.findAllByUser(userid);
			return SUCCESS;
		}
		if (LForumRequest.getParamValue("add").equals("true")) {
			infoList = infoManager.findAllByUser(userid);
			@SuppressWarnings("unused")
			FCKeditorWrapper fck = new FCKeditorWrapper(request);
			/*
			 * editor=fck.get("article.content", "Some text");
			 */
			if (LForumRequest.getParamIntValue("id", -1) != -1) {
				info = infoManager
						.get(LForumRequest.getParamIntValue("id", -1));
			} else {
				info = new Info();
			}
			fartherType = parentTypeManager.get(Integer.valueOf(pid))
					.getFartherType();
			return LOGIN;
		}
		if (LForumRequest.getParamValue("remove").equals("true")) {
			infoManager.remove(LForumRequest.getParamIntValue("id", -1));
			response.sendRedirect("addInfo.action?list=true");
			return null;
		}
		if (info != null) {
			info.setUser(userManager.getUserInfo(userid));
			if (fileUpload!= null) {
				String temp = DateUtils.getfileName();
				FileInputStream fis = new FileInputStream(getFileUpload());
				File file = new File(getFileSavePath());
				if (!file.exists()) {
					file.mkdirs();
				}
				FileOutputStream fos = new FileOutputStream(getFileSavePath()
						+ "/" + temp + getFileUploadFileName());
				int i = 0;
				byte[] buf = new byte[1024];
				while ((i = fis.read(buf)) != -1) {
					fos.write(buf, 0, i);
				}
				info.setUrl("UserFiles/"+temp + getFileUploadFileName());
			}
			infoManager.save(info);
			response.sendRedirect("addInfo.action?list=true");
			return null;
		}
		fartherlist = fartherTypeManager.findByType("0");
		return INPUT;
	}
	public String getFileSavePath() {
		return ServletActionContext.getServletContext().getRealPath(
				"/UserFiles");
	}

	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	public String getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(String fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

	public String getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(String fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}

	public ParentType getParentType() {
		return parentType;
	}

	public void setParentType(ParentType parentType) {
		this.parentType = parentType;
	}

	public Info getInfo() {
		return info;
	}

	public void setInfo(Info info) {
		this.info = info;
	}

	public List<Info> getInfoList() {
		return infoList;
	}

	public void setInfoList(List<Info> infoList) {
		this.infoList = infoList;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public InfoManager getInfoManager() {
		return infoManager;
	}

	public void setInfoManager(InfoManager infoManager) {
		this.infoManager = infoManager;
	}

	public FartherTypeManager getFartherTypeManager() {
		return fartherTypeManager;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setFartherTypeManager(FartherTypeManager fartherTypeManager) {
		this.fartherTypeManager = fartherTypeManager;
	}

	public List<FartherType> getFartherlist() {
		return fartherlist;
	}

	public void setFartherlist(List<FartherType> fartherlist) {
		this.fartherlist = fartherlist;
	}

	public ParentTypeManager getParentTypeManager() {
		return parentTypeManager;
	}

	public void setParentTypeManager(ParentTypeManager parentTypeManager) {
		this.parentTypeManager = parentTypeManager;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public List<ParentType> getParentTypelist() {
		return parentTypelist;
	}

	public void setParentTypelist(List<ParentType> parentTypelist) {
		this.parentTypelist = parentTypelist;
	}

	public FartherType getFartherType() {
		return fartherType;
	}

	public void setFartherType(FartherType fartherType) {
		this.fartherType = fartherType;
	}

}
